//工具函数
import { IndexShowData } from "@/api/interface";
const tool = {
  /**
   *将拿到的文章数据整理成Archive页面所需格式
   * @param arr 待处理包含对象的数组
   * @returns archive页面所需格式
   */
  handleArchiveArrData: (arr: Array<IndexShowData>) => {
    if (!Array.isArray(arr) || arr.length === 0) {
      return [];
    }

    const result = arr.reduce((acc: any, cur) => {
      if (!cur.id || !cur.date) {
        return acc;
      }

      const dateMatch = cur.date.match(/(\d{4})\s*\D+\s*(\d{1,2})/);
      if (!dateMatch) {
        return acc;
      }

      const year = dateMatch[1];
      const month = parseInt(dateMatch[2], 10);
      if (!month || isNaN(month)) {
        return acc;
      }

      const exist: any = acc.find(
        (item: { time: string }) => item.time === `${year}年${month}月`
      );
      if (exist) {
        exist.data.push(cur);
      } else {
        acc.push({ time: `${year}年${month}月`, data: [cur] });
      }

      return acc;
    }, []);

    return result;
  },
  dateMonth: [
    {
      value: "all",
      label: "全部月份",
      disabled: false,
    },
    {
      value: "01",
      label: "一月",
      disabled: false,
    },
    {
      value: "02",
      label: "二月",
      disabled: false,
    },
    {
      value: "03",
      label: "三月",
      disabled: false,
    },
    {
      value: "04",
      label: "四月",
      disabled: false,
    },
    {
      value: "05",
      label: "五月",
      disabled: false,
    },
    {
      value: "06",
      label: "六月",
      disabled: false,
    },
    {
      value: "07",
      label: "七月",
      disabled: false,
    },
    {
      value: "08",
      label: "八月",
      disabled: false,
    },
    {
      value: "09",
      label: "九月",
      disabled: false,
    },
    {
      value: "10",
      label: "十月",
      disabled: false,
    },
    {
      value: "11",
      label: "十一月",
      disabled: false,
    },
    {
      value: "12",
      label: "十二月",
      disabled: false,
    },
  ],
  //end
};
export default tool;
